|
|
|
|
|
|
|
application project, you have created another object or instance of a class. Another way to create an object requires you to add some code to your application. First, you must define the object in the declarations section of the application as shown in the following: |
|
|
|
|
|
|
|
|
Public myForm as NEW frmNote |
|
|
|
|
|
|
|
|
This declaration does not actually create an object until you access this variable in your application code. If you look at the code for the mnuFileNew_Click routine (shown in the following code) in the MDI project you created earlier in this book, you would see that every time you selected File New from the menu, a new instance of the form object was created: |
|
|
|
|
|
|
|
|
Private Sub mnuFileNew_Click()
Dim NewNote As New frmNote
intFormCtr = intFormCtr + 1
NewNote.Show
NewNote.Caption = NotePad Version & intFormCtr
End Sub |
|
|
|
|
|
|
|
|
The third way to create an object is also done in code using the SET command along with the NEW keyword, as shown in the following: |
|
|
|
|
|
|
|
|
Again, this does not actually create a new object until you reference the MyObj variable later in the code. This would enable you to create objects without having to define unique variables for each type of object you might create. So, the mnuFileNew_Click routine could be changed to the following: |
|
|
|
|
|
|
|
|
Private Sub mnuFileNew_Click()
intFormCtr = intFormCtr + 1
Set NewNoteObj = New frmNote
NewNote.Show
NewNote.Caption = NotePad Version & intFormCtr
End Sub |
|
|
|
|
|
|
|
|
Note When you are finished working with an object, it is good programming style to set the object to nothing, as shown here, to release the memory it was using: |
|
|
|
| | Set <object> = Nothing |
|
|
|
|
|
The NewNoteObj variable is defined in the common module (.BAS) as: |
|
|
|
|
|
|
|
|
Public NewNoteObj as Object |
|
|
|
|
|